package com.cy.store.service.Impl;

import com.cy.store.dto.UserDTO;
import com.cy.store.entity.User;
import com.cy.store.mapper.UserMapper;
import com.cy.store.service.UserService;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.PasswordErrorException;
import com.cy.store.service.ex.UserNameOccupiedException;
import com.cy.store.service.ex.UserNotFoundException;
import com.cy.store.util.Md5Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    Md5Method md5Method=new Md5Method();
    /***注册业务实现
     *
     * @param userDTO
     */
    @Override
    public void reg(UserDTO userDTO) {
        String regUserName=userDTO.getUsername();
        User regUser=new User();
        User dataUser=userMapper.getByUserName(regUserName);
        if(dataUser!= null){
            throw new UserNameOccupiedException("用户名被占用");
        }

        //随机获取盐值
        String salt= UUID.randomUUID().toString().toUpperCase();
        log.info("获取到的新盐值为:{}",salt);

        //md5加密
        String md5Password=md5Method.getMD5Password(userDTO.getPassword(),salt);

        //将新密码更新到UserDTO对象中
        userDTO.setPassword(md5Password);

        //将DTO的值赋给实体类对象并补充其他信息
        BeanUtils.copyProperties(userDTO,regUser);
        regUser.setSalt(salt);
        regUser.setIsDelete(0);
        regUser.setCreatedUser(regUserName);
        Date date=new Date();
        regUser.setCreatedTime(date);
        regUser.setModifiedUser(regUserName);
        regUser.setModifiedTime(date);

        int row=userMapper.insert(regUser);
        log.info("插入影响行数:{}",row);
        if(row < 1){
            throw new InsertException("插入时出现异常");
        }else {
            log.info("注册信息:{}","注册成功");
        }
    }

    /***登录业务实现
     *
     * @param userDTO
     */
    @Override
    public User login(UserDTO userDTO) {
        String loginUsername = userDTO.getUsername();
        String loginPassword = userDTO.getPassword();

        User DateUser = userMapper.getByUserName(loginUsername);

        //判断用户名是否存在
        if (DateUser == null) {
            throw new UserNotFoundException("用户名不存在");
        }

        //获取数据库用户的盐值
        String DateSalt = DateUser.getSalt();

        //新密码盐值加密
        String md5Password = md5Method.getMD5Password(loginPassword, DateSalt);
        log.info("加密后新密码为:{}", md5Password);
        log.info("数据库密码为:{}", DateUser.getPassword());


        //新旧密码对比
        if (!md5Password.equals(DateUser.getPassword())) {
            throw new PasswordErrorException("密码错误");
        }

            User user = new User();
            user.setUid(DateUser.getUid());
            user.setUsername(DateUser.getUsername());
            user.setAvatar(DateUser.getAvatar());
            return user;
        }
}
